Component({
  behaviors: [],
  properties: {
    showIndex: {
      type: String,
      value: '0'
    }
  },
  data: {
    current: 0,
    active: 0,
    timeActive: 0,
    jobActive: 0,
    gbActive: 0,
    areas: [],
    times: [
      {
        "id": 0,
        "name": "全部"
      },
      {
        "id": 1,
        "name": "仅三天"
      },
      {
        "id": 2,
        "name": "近七天"
      },
      {
        "id": 3,
        "name": "近一月"
      },
      {
        "id": 4,
        "name": "近三月"
      },
      {
        "id": 5,
        "name": "近一年"
      }
    ],
    jobs: [],
    gbs: [
      {
        "id": 0,
        "name": "不限"
      },
      {
        "id": 1,
        "name": "招标公告"
      },
      {
        "id": 2,
        "name": "意向公开"
      },
      {
        "id": 3,
        "name": "在建公告"
      },{
        "id": 4,
        "name": "中标公告"
      },
      {
        "id": 5,
        "name": "调研公告"
      },
      {
        "id": 6,
        "name": "合同公告"
      },
      {
        "id": 7,
        "name": "其他公告"
      }
    ],

    pickerShow: false,
    startDay: "",
    endDay: "",
  },
  lifetimes: {
    created() {
      let arr = wx.getStorageSync('myArea') || [];
      let arr2 = wx.getStorageSync('myIndustry') || [];
      this.setData({
        areas: arr,
        jobs: arr2
      })
    },
    attached() {
      this.setData({
        current: this.properties.showIndex
      })
    },
    moved() {
    },
    detached() {
    },
  },
  methods: {
    showPicker() {
      this.setData({ pickerShow: true });
    },
    hidePicker() {
      this.setData({ pickerShow: false });
    },
    onConfirm(event) {
      const [start, end] = event.detail;  
      this.setData({
        pickerShow: false,
        startDay: this.dealTime(start),
        endDay: this.dealTime(end),
      });
    },
    changeCurrent(event) {
      this.setData({
        current: event.currentTarget.dataset.current
      })
    },
    tapArea(event) {
      console.log('tapArea === ', event.currentTarget.dataset.index)
      this.setData({
        active: event.currentTarget.dataset.index
      })
    },
    tapJob(event) {
      this.setData({
        jobActive: event.currentTarget.dataset.index
      })
    },
    tapGb(event) {
      this.setData({
        gbActive: event.currentTarget.dataset.index
      })
    },
    tapTime(event) {
      let index = event.currentTarget.dataset.index;
      let time = this.data.times[index];
      console.log('time === ', time)
      let date = new  Date()

      let ed = this.dealTime(date);
      let sd = '';
      if (time.id == 0) {
        ed = '';
        sd = '';
      } else if (time.id == 1) {
        date.setDate(date.getDate()-3);
        console.log(12312)
      } else if (time.id == 2) {
        date.setDate(date.getDate()-7);
      } else if (time.id == 3) {
        date.setDate(date.getDate()-30);
      } else if (time.id == 4) {
        date.setDate(date.getDate()-90);
      } else if (time.id == 5) {
        date.setDate(date.getDate()-365);
      }
      sd = this.dealTime(date);

      this.setData({
        timeActive: event.currentTarget.dataset.index,
        startDay: sd,
        endDay: ed
      })
    },
    reset() {
      this.setData({
        active: 0,
        jobActive: 0,
        gbActive: 0,
        timeActive: 0
      })
    },
    dealTime(date) {
      let m = date.getMonth()+1;
      let d = date.getDate();
      if (m < 10) {
        m = '0'+m
      }
      if (d < 10) {
        d = '0'+d
      }
      return date.getFullYear()+'-'+m+'-'+d;
    },
    dealDate() {
      let startDay = this.dealTime(date);
      return [startDay, endDay];
    },
    tapConfirm() {
      let address = this.data.areas[this.data.active].name;
      if (address == '不限') {
        address = '';
      }
      let industry = this.data.jobs[this.data.jobActive].name;
      if (industry == '不限') {
        industry = '';
      }
      this.triggerEvent('tapConfirm', {
        address,industry,
        start_create_date: this.data.startDay,
        end_create_date: this.data.endDay,
      });
    }
  },
});